package com.example.activity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.activity.constant.FileModuleRelTableEnum;
import com.example.activity.mapper.FileInfoRelMapper;
import com.example.activity.po.FileInfoRelPO;
import com.example.activity.service.FileInfoRelService;
import org.springframework.stereotype.Service;

@Service
public class FileInfoRelServiceImpl extends ServiceImpl<FileInfoRelMapper, FileInfoRelPO>
        implements FileInfoRelService {


    /**
     * 新增或者更新关联关系
     *
     * @param relTableEnum 关联表枚举
     * @param fileInfoId 文件id
     * @param tableIdStr 关联主键
     * @return FileTableRelPO
     */
    @Override
    public FileInfoRelPO saveOrUpdateFileTableRel(FileModuleRelTableEnum relTableEnum, Long fileInfoId, String tableIdStr) {

        FileInfoRelPO relPO = this.lambdaQuery()
                // 模块名称
                .eq(FileInfoRelPO::getModuleName, relTableEnum.getModuleName())
                // 表名称
                .eq(FileInfoRelPO::getTableName, relTableEnum.getTableName())
                // 字段名称
                .eq(FileInfoRelPO::getTableColumn, relTableEnum.getTableColumn())
                .eq(FileInfoRelPO::getFileInfoId, fileInfoId)
                .eq(FileInfoRelPO::getTableIdStr, tableIdStr)
                .one();


        // 赋值
        if (relPO == null) {
            relPO = new FileInfoRelPO();
        }
        relPO.setModuleName(relTableEnum.getModuleName());
        relPO.setTableName(relTableEnum.getTableName());
        relPO.setTableColumn(relTableEnum.getTableColumn());
        relPO.setFileInfoId(fileInfoId);
        relPO.setTableIdStr(tableIdStr);

        // 新增或者更新关联字段
        this.saveOrUpdate(relPO);
        return relPO;
    }
}
